home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / game / demo / GDS_MISC.lha / misc / vector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-15  |  7.5 KB  |  302 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <exec/types.h>
  5. #include <graphics/gfx.h>
  6. #include <graphics/gfxbase.h>
  7.  
  8. #include <proto/exec.h>
  9. #include <proto/graphics.h>
  10.  
  11. #include "GameSmith:GameSmith.h"
  12. #include "GameSmith:include/libraries/libptrs.h"
  13.  
  14. /*-------------------------------------------------------------------------*/
  15.  
  16. #define VIDEO_STEPS        30
  17. #define COLOR_GRADIENT    0x100                /* color change amount */
  18. #define VIDEO_REG            0x180                /* address of bg color reg */
  19. #define BGCOLOR            0xf00                /* max color value (bright red) */
  20. #define BGCOLOR_LONG        0x00ff0000        /* 8 bit color entry (bright red) */
  21.  
  22. #define THRESHOLD            1000                /* number of lines before clear screen */
  23.  
  24. /*-------------------------------------------------------------------------*/
  25. /* Function Prototypes                                                                       */
  26.  
  27. void parser(int,char **);
  28. int setup(void);
  29. void build_copper(void);
  30. void draw_vector(void);
  31. void clear_bitmap(struct BitMap *);
  32. int check_close(void);
  33. void cleanup(void);
  34.  
  35. /*-------------------------------------------------------------------------*/
  36. /* some global variables                                                   */
  37.  
  38. int swidth,sheight,smode;
  39.  
  40. /*-------------------------------------------------------------------------*/
  41.  
  42. unsigned short copper_list[256];    /* enough for our custom copper list */
  43.  
  44. struct copper_struct copper = 
  45.     {
  46.     copper_list,
  47.     NULL,
  48.     NULL
  49.     };
  50.  
  51. unsigned long ctbl[2] = {BGCOLOR_LONG,0x00ffff00};
  52.  
  53. struct gs_viewport vp =
  54.     {
  55.     NULL,                                    /* ptr to next viewport */
  56.     ctbl,                                    /* ptr to color table */
  57.     2,                                        /* number of colors in table */
  58.     &copper,                                /* ptr to user copper list */
  59.     0,0,0,0,0,                            /* height, width, depth, bmheight, bmwidth */
  60.     0,0,                                    /* top & left viewport offsets */
  61.     0,0,                                    /* X & Y bitmap offsets */
  62.     GSVP_ALLOCBM,                        /* flags (alloc bitmap) */
  63.     NULL,NULL,                            /* 2.xx & above compatibility stuff */
  64.     NULL,NULL,                            /* bitmap pointers */
  65.     NULL,                                    /* future expansion */
  66.     0,0,0,0                                /* display clip (use nominal) */
  67.     };
  68.  
  69. struct display_struct vector_display =
  70.     {
  71.     NULL,                                    /* ptr to previous display view */
  72.     NULL,NULL,                            /* 2.xx & above compatibility stuff */
  73.     0,0,                                    /* X and Y display offsets */
  74.     0,                                        /* display mode ID */
  75.     0,                                        /* flags */
  76.     &vp,                                    /* ptr to 1st viewport */
  77.     NULL                                    /* future expansion */
  78.     };
  79.  
  80. /***************************************************************************/
  81.  
  82. main(argc,argv)
  83. int argc;
  84. char *argv[];
  85.  
  86. {
  87.     int err,end=0;
  88.  
  89.     if (gs_open_libs(GRAPHICS,0))    /* open AmigaDOS libs */
  90.         exit(01);                    /* if can't open libs, abort */
  91.     parser(argc,argv);            /* parse command line args */
  92.     if (err=setup())                /* if couldn't get set up... abort program */
  93.         {
  94.         printf("\nSetup error: %d\n",err);
  95.         gs_close_libs();            /* close all libraries */
  96.         exit(02);
  97.         }
  98.     Forbid();                        /* take over the entire machine */
  99.     while (!end)                    /* this shows off speed */
  100.         {
  101.         draw_vector();
  102.         gs_show_display(&vector_display,1);    /* don't let mouse blanker mess us up */
  103.         end=check_close();        /* end when user hits left mouse button */
  104.         }
  105.     Permit();                        /* OK, let other things run while we clean up */
  106.     cleanup();                        /* close & deallocate everything */
  107.     gs_close_libs();                /* close all libraries */
  108. }
  109.  
  110. /***************************************************************************/
  111.  
  112. void parser(argc,argv)
  113. int argc;
  114. char *argv[];
  115.  
  116. {
  117.     swidth=320;                        /* default width & height */
  118.     sheight=200;
  119.     smode=0;                            /* default mode of lores no lace */
  120.     if ((argc >= 2) && (!(strcmp(argv[1],"?"))))
  121.         {
  122.         printf("\nUSAGE: vector [HIRES] [SUPER]\n");
  123.         return;
  124.         }
  125.     else if (argc >= 2)
  126.         {
  127.         if (!(stricmp(argv[1],"HIRES")))    /* check for hires spec */
  128.             {
  129.             swidth=640;
  130.             sheight=400;
  131.             if (GfxBase->LibNode.lib_Version >= 36)
  132.                 {
  133.                 if (ModeNotAvailable(DBLNTSCHIRESFF_KEY))
  134.                     smode=HIRES|LACE;
  135.                 else
  136.                     smode=DBLNTSCHIRESFF_KEY;
  137.                 }
  138.             else
  139.                 smode=HIRES|LACE;
  140.             }
  141.         else if (!(stricmp(argv[1],"SUPER")))    /* check for superhires */
  142.             {
  143.             if (GfxBase->LibNode.lib_Version >= 36)
  144.                 {
  145.                 if (ModeNotAvailable(SUPER72_MONITOR_ID | SUPERLACE_KEY))
  146.                     {
  147.                     swidth=640;
  148.                     sheight=400;
  149.                     smode=HIRES|LACE;
  150.                     }
  151.                 else
  152.                     {
  153.                     smode=SUPER72_MONITOR_ID | SUPERLACE_KEY;
  154.                     swidth=800;
  155.                     sheight=600;
  156.                     }
  157.                 }
  158.             else
  159.                 {
  160.                 swidth=640;
  161.                 sheight=400;
  162.                 smode=HIRES|LACE;
  163.                 }
  164.             }
  165.         }
  166. }
  167.  
  168. /***************************************************************************/
  169.  
  170. int setup()
  171.  
  172. {
  173.     vp.height = sheight;                    /* set up display dimensions */
  174.     vp.width = swidth;
  175.     vp.depth = 1;
  176.     vp.bmheight = sheight;
  177.     vp.bmwidth = swidth;
  178.     vector_display.modes = smode;
  179.     build_copper();                        /* build custom copper list */
  180.     if (gs_create_display(&vector_display))
  181.         {
  182.         return(-1);
  183.         }
  184.     gs_show_display(&vector_display,1);
  185.     return(0);
  186. }
  187.  
  188. /***************************************************************************/
  189.  
  190. void build_copper()
  191.  
  192. /* build a custom copper list of background color changes */
  193.  
  194. {
  195.     int cnt,cnt2=0,video_gradient;
  196.     short bgcolor,gradient;
  197.  
  198.     bgcolor=BGCOLOR;
  199.     gradient=0;
  200.     video_gradient=(sheight/3)/15;
  201.     copper_list[cnt2++]=UC_NOSPRITES;        /* turn off sprites */
  202.     for (cnt=0; cnt < 16; cnt++)                /* build copper list */
  203.         {
  204.         if (cnt)
  205.             {
  206.             copper_list[cnt2++]=UC_WAIT;        /* copper wait instruction */
  207.             copper_list[cnt2++]=gradient;        /* y coord to wait on */
  208.             copper_list[cnt2++]=0;                /* x coord to wait on */
  209.             }
  210.         copper_list[cnt2++]=UC_MOVE;            /* copper move instruct */
  211.         copper_list[cnt2++]=VIDEO_REG;        /* register to affect */
  212.         copper_list[cnt2++]=bgcolor;            /* value for register */
  213.         gradient+=video_gradient;
  214.         bgcolor-=COLOR_GRADIENT;
  215.         if (bgcolor < 0)
  216.             bgcolor=0;
  217.         }
  218.     gradient=(sheight/3)*2;                        /* build bottom color gradient */
  219.     bgcolor+=COLOR_GRADIENT;
  220.     for (cnt=0; cnt < 15; cnt++)                /* build copper list */
  221.         {
  222.         copper_list[cnt2++]=UC_WAIT;            /* copper wait instruction */
  223.         copper_list[cnt2++]=gradient;            /* y coord to wait on */
  224.         copper_list[cnt2++]=0;                    /* x coord to wait on */
  225.         copper_list[cnt2++]=UC_MOVE;            /* copper move instruct */
  226.         copper_list[cnt2++]=VIDEO_REG;        /* register to affect */
  227.         copper_list[cnt2++]=bgcolor;            /* value for register */
  228.         gradient+=video_gradient;
  229.         bgcolor+=COLOR_GRADIENT;
  230.         if (gradient > sheight)
  231.             gradient=sheight;
  232.         if (bgcolor > BGCOLOR)
  233.             bgcolor=BGCOLOR;
  234.         }
  235.     copper_list[cnt2++]=UC_END;                /* end coppper list */
  236. }
  237.  
  238. /***************************************************************************/
  239.  
  240. void draw_vector()
  241.  
  242. /* draw a line on the screen */
  243.  
  244. {
  245.     static int line_count=0;
  246.     int x1,y1,cnt;
  247.  
  248.     for (cnt=0; cnt < 100; cnt++)
  249.         {
  250.         x1=gs_random(swidth);
  251.         y1=gs_random(sheight);
  252.         gs_init_vector(0,x1,y1,gs_random(swidth),gs_random(sheight));
  253.         while (x1|y1)
  254.             {
  255.             gs_plot(vp.bitmap1,x1,y1,1);
  256.             gs_step_vector(0,1,&x1,&y1);        /* next point on line */
  257.             }
  258.         }
  259.     line_count+=100;
  260.     if (line_count >= THRESHOLD)
  261.         {
  262.         clear_bitmap(vp.bitmap1);
  263.         line_count=0;
  264.         }
  265. }
  266.  
  267. /***************************************************************************/
  268.  
  269. void clear_bitmap(bitmap)
  270. struct BitMap *bitmap;
  271.  
  272. {
  273.     int cnt;
  274.  
  275.     for (cnt=0; cnt < bitmap->Depth; cnt++)
  276.         {            /* clear each plane, wait for blitter to finish */
  277.         BltClear(bitmap->Planes[cnt],(bitmap->Rows*bitmap->BytesPerRow),1);
  278.         }
  279. }
  280.  
  281. /***************************************************************************/
  282.  
  283. int check_close()
  284.  
  285. /* check for user input */
  286.  
  287. {
  288.     if (gs_joystick(0) & JOY_BUTTON1)
  289.         return(1);
  290.     return(0);
  291. }
  292.  
  293. /***************************************************************************/
  294.  
  295. void cleanup()
  296.  
  297. /* release all resources and memory */
  298.  
  299. {
  300.     gs_remove_display(&vector_display);
  301. }
  302.